package com.prs.crm.dao.sys.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.sys.LogDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.sys.Log;
import com.prs.crm.domain.sys.LogItem;

public class LogDaoImpl extends BaseDaoImpl<Log> implements LogDao {

	public void saveLogItem(LogItem item) {
		this.getHibernateTemplate().save(item);
	}

	@Override
	public boolean getSearchName(Class class1, String name) {
		Criteria criteria = this.getSession().createCriteria(class1);
		int i = criteria.add(Restrictions.eq("name", name)).list().size();
		if (i>0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean getSearchName1(Class class1, String type, String name) {
		Criteria criteria = this.getSession().createCriteria(class1);
		int i = criteria.add(Restrictions.eq(type, name)).list().size();
		if (i>0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Integer delLog() {
		return (Integer) this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
							Query query = session.createQuery("delete from Log");
							return query.executeUpdate();
				        }   
				 } );
	}

	@Override
	public List<Log> ByPersons(final Person per) {
		 return (List<Log>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(Log.class);
				        	criteria.add(Restrictions.eq("operator",per));
				        
							return criteria.list();
				 				        	
				        }
					}
			);
	}
}
